package com.woniu.bh.beautysalon.web.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.bh.beautysalon.model.dto.AllServiceDto;
import com.woniu.bh.beautysalon.model.dto.ServiceRankDto;
import com.woniu.bh.beautysalon.model.params.NewServiceParam;
import com.woniu.bh.beautysalon.web.dao.BeautysalonServiceMapper;
import com.woniu.bh.beautysalon.web.service.IBeautysalonServiceService;
import com.woniu.bh.common.core.domain.beautysalon.BeautysalonService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author daniel
 * @since 2020-10-15
 */
@Service
public class BeautysalonServiceServiceImpl extends ServiceImpl<BeautysalonServiceMapper, BeautysalonService> implements IBeautysalonServiceService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private BeautysalonServiceMapper bsm;
    /**
     * @description: 服务排行榜
     * @param: beauysalonId
     * @return: com.woniu.bh.beautysalon.model.dto.ServiceRankDto
     */
    @Override
    public ServiceRankDto serviceRank(Integer beauysalonId) {
        QueryWrapper<BeautysalonService> bsqw = new QueryWrapper<>();
        bsqw.eq("service_beautysalon_id",beauysalonId).orderByDesc("service_times").eq("service_status","up");
        List<BeautysalonService> beautysalonServices = bsm.selectList(bsqw);
        ServiceRankDto serviceRankDto = new ServiceRankDto();
        List<BeautysalonService> bsl = new ArrayList<>(beautysalonServices);
        serviceRankDto.setBeautysalonServices(bsl);
        return serviceRankDto;
    }



    /**
     * @description: 查询美容馆所有的服务
     * @param: pageIndex
     * @param: pageSize
     * @param: beauysalonId
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<AllServiceDto>
     */
    @Override
    public IPage<AllServiceDto> getAllService(Integer pageIndex, Integer pageSize, Integer beautysalonId) {
        Page<AllServiceDto> page = new Page<>(pageIndex, pageSize);
        return bsm.getAllService(page,beautysalonId);
    }

    /**
     * @description: 新增服务
     * @param: newServiceParam
     * @param: beautysalonId
     * @return: int
     */
    @Override
    public int addNewService(NewServiceParam newServiceParam, Integer beautysalonId) {
        BeautysalonService beautysalonService = new BeautysalonService();
        BeanUtils.copyProperties(newServiceParam,beautysalonService);
        beautysalonService.setServiceBeautysalonId(beautysalonId);
        beautysalonService.setServiceShelvesTime(System.currentTimeMillis());
        beautysalonService.setServiceStatus("up");
        beautysalonService.setServiceTimes(0);
        beautysalonService.setServiceUpdateTime(System.currentTimeMillis());
        String fileUrl = stringRedisTemplate.opsForValue().get("servicePic");
        beautysalonService.setServicePic(fileUrl);
        bsm.insert(beautysalonService);
        return beautysalonService.getServiceId();
    }

    /**
     * @description: 下架
     * @param: serviceId
     * @return: int
     */
    @Override
    public int downService(Integer serviceId) {
        BeautysalonService beautysalonService = new BeautysalonService();
        beautysalonService.setServiceId(serviceId);
        beautysalonService.setServiceStatus("down");
        return bsm.updateById(beautysalonService);
    }
}
